package HashTable;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

public class _500_KeyboardRow {
    public String[] findWords(String[] words) {
        String[] rows = {"qwertyuiop","asdfghjkl","zxcvbnm"};
        List<String> result = new ArrayList<String>();
        for(int i=0;i<words.length;i++) {
            String temp  =words[i];
            int pos = -1;
            boolean valid = true;
            for (char c : temp.toCharArray()) {
                c = Character.toLowerCase(c);
                int temppos = -1;
                if (rows[0].indexOf(c) != -1) {
                    temppos = 0;
                } else if (rows[1].indexOf(c) != -1) {
                    temppos = 1;
                } else if (rows[2].indexOf(c) != -1) {
                    temppos = 2;
                }
                if (pos == -1) {
                    pos = temppos;
                } else {
                    if (temppos != pos) {
                        valid = false;
                        break;
                    }
                }
            }
            if (valid) {
                result.add(temp);
            }
        }
        return result.toArray(new String[0]);
    }
}
